package com.ruoyi.project.modules.virtualUsageGroupDetail.controller;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.project.modules.interfaceinfo.service.IInterfaceInfoService;
import com.ruoyi.project.modules.interfaceinfo.vo.InterfaceParam;
import com.ruoyi.project.modules.parameter.domain.InterfaceParamsFieldRelation;
import com.ruoyi.project.modules.parameter.domain.ParamsFieldRelation;
import com.ruoyi.project.modules.parameter.service.IInterfaceParamsFieldRelationService;
import com.ruoyi.project.modules.parameter.service.IParamsFieldRelationService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.project.modules.virtualUsageGroupDetail.domain.VirtualUsageGroupDetail;
import com.ruoyi.project.modules.virtualUsageGroupDetail.service.IVirtualUsageGroupDetailService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 虚拟用况组明细Controller
 *
 * @author smallrain
 * @date 2022-04-19
 */
@Controller
@RequestMapping("/virtualUsageGroupDetail/virtualUsageGroupDetail")
public class VirtualUsageGroupDetailController extends BaseController {
    private String prefix = "virtualUsageGroupDetail/virtualUsageGroupDetail";

    @Autowired
    private IVirtualUsageGroupDetailService virtualUsageGroupDetailService;

    @RequiresPermissions("virtualUsageGroupDetail:virtualUsageGroupDetail:view")
    @GetMapping()
    public String virtualUsageGroupDetail() {
        return prefix + "/virtualUsageGroupDetail";
    }

    /**
     * 查询虚拟用况组明细列表
     */
    @RequiresPermissions("virtualUsageGroupDetail:virtualUsageGroupDetail:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(VirtualUsageGroupDetail virtualUsageGroupDetail) {
        startPage();
        List<VirtualUsageGroupDetail> list = virtualUsageGroupDetailService.selectVirtualUsageGroupDetailList(virtualUsageGroupDetail);
        return getDataTable(list);
    }

    /**
     * 导出虚拟用况组明细列表
     */
    @RequiresPermissions("virtualUsageGroupDetail:virtualUsageGroupDetail:export")
    @Log(title = "虚拟用况组明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(VirtualUsageGroupDetail virtualUsageGroupDetail) {
        List<VirtualUsageGroupDetail> list = virtualUsageGroupDetailService.selectVirtualUsageGroupDetailList(virtualUsageGroupDetail);
        ExcelUtil<VirtualUsageGroupDetail> util = new ExcelUtil<VirtualUsageGroupDetail>(VirtualUsageGroupDetail.class);
        return util.exportExcel(list, "虚拟用况组明细数据");
    }

    /**
     * 新增虚拟用况组明细
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存虚拟用况组明细
     */
    @RequiresPermissions("virtualUsageGroupDetail:virtualUsageGroupDetail:add")
    @Log(title = "虚拟用况组明细", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(VirtualUsageGroupDetail virtualUsageGroupDetail) {
        return toAjax(virtualUsageGroupDetailService.insertVirtualUsageGroupDetail(virtualUsageGroupDetail));
    }

    @Log(title = "虚拟用况组明细", businessType = BusinessType.INSERT)
    @PostMapping("/saveVirtualUsage")
    @ResponseBody
    public AjaxResult saveVirtualUsage(VirtualUsageGroupDetail virtualUsageGroupDetail) {
        return toAjax(virtualUsageGroupDetailService.saveVirtualUsage(virtualUsageGroupDetail));
    }

    @Log(title = "虚拟用况组明细", businessType = BusinessType.INSERT)
    @PostMapping("/updateInterfaceSort")
    @ResponseBody
    public AjaxResult updateInterfaceSort(@RequestBody VirtualUsageGroupDetail virtualUsageGroupDetail) {
        return toAjax(virtualUsageGroupDetailService.saveVirtualUsage(virtualUsageGroupDetail));
    }

    /**
     * 修改虚拟用况组明细
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        VirtualUsageGroupDetail virtualUsageGroupDetail = virtualUsageGroupDetailService.selectVirtualUsageGroupDetailById(id);
        mmap.put("virtualUsageGroupDetail", virtualUsageGroupDetail);
        return prefix + "/edit";
    }

    /**
     * 修改保存虚拟用况组明细
     */
    @RequiresPermissions("virtualUsageGroupDetail:virtualUsageGroupDetail:edit")
    @Log(title = "虚拟用况组明细", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(VirtualUsageGroupDetail virtualUsageGroupDetail) {
        return toAjax(virtualUsageGroupDetailService.updateVirtualUsageGroupDetail(virtualUsageGroupDetail));
    }

    @Autowired
    private IInterfaceInfoService interfaceInfoService;

    @Autowired
    private IInterfaceParamsFieldRelationService interfaceParamsFieldRelationService;

    @Autowired
    private IParamsFieldRelationService paramsFieldRelationService;

    /**
     * 删除虚拟用况组明细
     */
    //@RequiresPermissions("virtualUsageGroupDetail:virtualUsageGroupDetail:remove")
    //@Log(title = "虚拟用况组明细", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids, Long virtualUsageGroupId) {
        long interfaceId = Long.parseLong(ids);
        List<InterfaceParam> interfaceParams1 = this.interfaceInfoService.paramList("5", virtualUsageGroupId, "1");
        List<InterfaceParam> interfaceParams2 = this.interfaceInfoService.paramList("5", virtualUsageGroupId, "2");
        List<Long> ids1 = new ArrayList<>();
        this.getInterfaceFieldId(ids1, interfaceParams1);
        this.getInterfaceFieldId(ids1, interfaceParams2);
        if (ids1.size() > 0) {
            VirtualUsageGroupDetail virtualUsageGroupDetail = this.virtualUsageGroupDetailService.selectVirtualUsageGroupDetailById(interfaceId);
            InterfaceParamsFieldRelation interfaceParamsFieldRelation = new InterfaceParamsFieldRelation();
            interfaceParamsFieldRelation.setIds(ids1);
            List<InterfaceParamsFieldRelation> interfaceParamsFieldRelationList = interfaceParamsFieldRelationService.selectInterfaceParamsFieldRelationList(interfaceParamsFieldRelation);
            if (CollectionUtil.isNotEmpty(interfaceParamsFieldRelationList)) {
                for (InterfaceParamsFieldRelation paramsFieldRelation : interfaceParamsFieldRelationList) {
                    if (paramsFieldRelation.getInterfaceId().equals(virtualUsageGroupDetail.getVirtualUsageId())) {
                        return AjaxResult.warn("被虚拟用况组字段引用，不能删除");
                    }
                }
            }
        }
        return toAjax(virtualUsageGroupDetailService.deleteVirtualUsageGroupDetailByIds(ids));
    }

    private void getInterfaceFieldId(List<Long> ids1, List<InterfaceParam> interfaceParams) {
        if (CollectionUtil.isNotEmpty(interfaceParams)) {
            for (InterfaceParam interfaceParam : interfaceParams) {
                if (interfaceParam.getDataRelationId() != null && interfaceParam.getDataRelationType() != null) {
                    if ("1".equals(interfaceParam.getDataRelationType())) {
                        ids1.add(interfaceParam.getDataRelationId());
                    } else {
                        ParamsFieldRelation paramsFieldRelation = this.paramsFieldRelationService.selectParamsFieldRelationById(interfaceParam.getDataRelationId());
                        if (paramsFieldRelation != null) {
                            ids1.add(paramsFieldRelation.getInterfaceDataListId());
                        }
                    }
                }
                this.getInterfaceFieldId(ids1, interfaceParam.getChildren());
            }
        }
    }
}
